<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Classes  Reference</title>
    <link rel="stylesheet" type="text/css" href="css/jazzy.css" />
    <link rel="stylesheet" type="text/css" href="css/highlight.css" />
    <meta charset='utf-8'>
    <script src="js/jquery.min.js" defer></script>
    <script src="js/jazzy.js" defer></script>
    
    <script src="js/lunr.min.js" defer></script>
    <script src="js/typeahead.jquery.js" defer></script>
    <script src="js/jazzy.search.js" defer></script>
  </head>
  <body>
    <a name="//apple_ref/swift/Section/Classes" class="dashAnchor"></a>
    <a title="Classes  Reference"></a>
    <header>
      <div class="content-wrapper">
        <p><a href="index.html">SwiftCoroutine 2.1.9 Docs</a> (88% documented)</p>
        <p class="header-right">
          <form role="search" action="search.json">
            <input type="text" placeholder="Search documentation" data-typeahead>
          </form>
        </p>
      </div>
    </header>
    <div class="content-wrapper">
      <p id="breadcrumbs">
        <a href="index.html">SwiftCoroutine Reference</a>
        <img id="carat" src="img/carat.png" />
        Classes  Reference
      </p>
    </div>
    <div class="content-wrapper">
      <nav class="sidebar">
        <ul class="nav-groups">
          <li class="nav-group-name">
            <a href="Classes.html">Classes</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Classes/CoChannel.html">CoChannel</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/CoChannel/BufferType.html">– BufferType</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/CoChannel/Receiver.html">– Receiver</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/CoChannel/Sender.html">– Sender</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/CoFuture.html">CoFuture</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/CoPromise.html">CoPromise</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/CoScope.html">CoScope</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Enums.html">Enumerations</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Enums/CoChannelError.html">CoChannelError</a>
              </li>
              <li class="nav-group-task">
                <a href="Enums/CoFutureError.html">CoFutureError</a>
              </li>
              <li class="nav-group-task">
                <a href="Enums/CoroutineError.html">CoroutineError</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Extensions.html">Extensions</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Extensions/DispatchQueue.html">DispatchQueue</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/Publisher.html">Publisher</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/URLSession.html">URLSession</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Protocols.html">Protocols</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Protocols/CoCancellable.html">CoCancellable</a>
              </li>
              <li class="nav-group-task">
                <a href="Protocols/CoroutineScheduler.html">CoroutineScheduler</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Structs.html">Structures</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Structs/Coroutine.html">Coroutine</a>
              </li>
            </ul>
          </li>
        </ul>
      </nav>
      <article class="main-content">
        <section>
          <section class="section">
            <h1>Classes</h1>
            <p>The following classes are available globally.</p>

          </section>
          <section class="section task-group-section">
            <div class="task-group">
              <ul>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:14SwiftCoroutine9CoChannelC"></a>
                    <a name="//apple_ref/swift/Class/CoChannel" class="dashAnchor"></a>
                    <a class="token" href="#/s:14SwiftCoroutine9CoChannelC">CoChannel</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Channel is a non-blocking primitive for communication between a sender and a receiver.
Conceptually, a channel is similar to a queue that allows to suspend a coroutine on receive if it is empty or on send if it is full.</p>
<div class="aside aside-important">
    <p class="aside-title">Important</p>
    Always <code>close()</code> or <code>cancel()</code> a channel when you are done to resume all suspended coroutines by the channel.

</div>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">channel</span> <span class="o">=</span> <span class="kt">CoChannel</span><span class="o">&lt;</span><span class="kt">Int</span><span class="o">&gt;</span><span class="p">(</span><span class="nv">capacity</span><span class="p">:</span> <span class="mi">1</span><span class="p">)</span>

<span class="kt">DispatchQueue</span><span class="o">.</span><span class="nf">global</span><span class="p">()</span><span class="o">.</span><span class="n">startCoroutine</span> <span class="p">{</span>
   <span class="k">for</span> <span class="n">i</span> <span class="k">in</span> <span class="mi">0</span><span class="o">..&lt;</span><span class="mi">100</span> <span class="p">{</span>
       <span class="k">try</span> <span class="n">channel</span><span class="o">.</span><span class="nf">awaitSend</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
   <span class="p">}</span>
   <span class="n">channel</span><span class="o">.</span><span class="nf">close</span><span class="p">()</span>
<span class="p">}</span>

<span class="kt">DispatchQueue</span><span class="o">.</span><span class="nf">global</span><span class="p">()</span><span class="o">.</span><span class="n">startCoroutine</span> <span class="p">{</span>
    <span class="k">for</span> <span class="n">i</span> <span class="k">in</span> <span class="n">channel</span><span class="o">.</span><span class="nf">makeIterator</span><span class="p">()</span> <span class="p">{</span>
        <span class="nf">print</span><span class="p">(</span><span class="s">"Receive"</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span>
    <span class="p">}</span>
    <span class="nf">print</span><span class="p">(</span><span class="s">"Done"</span><span class="p">)</span>
<span class="p">}</span>
</code></pre>

                        <a href="Classes/CoChannel.html" class="slightly-smaller">See more</a>
                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">final</span> <span class="kd">class</span> <span class="kt">CoChannel</span><span class="o">&lt;</span><span class="kt">Element</span><span class="o">&gt;</span></code></pre>
<pre class="highlight swift"><code><span class="kd">extension</span> <span class="kt">CoChannel</span><span class="p">:</span> <span class="kt"><a href="Protocols/CoCancellable.html">CoCancellable</a></span></code></pre>

                        </div>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:14SwiftCoroutine8CoFutureC"></a>
                    <a name="//apple_ref/swift/Class/CoFuture" class="dashAnchor"></a>
                    <a class="token" href="#/s:14SwiftCoroutine8CoFutureC">CoFuture</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Holder for a result that will be provided later.</p>

<p><code>CoFuture</code> and its subclass <code><a href="Classes/CoPromise.html">CoPromise</a></code> are the implementation of the Future/Promise approach.
They allow to launch asynchronous tasks and immediately return<code>CoFuture</code> with its future results.
The available result can be observed by the <code>whenComplete()</code> callback
or by <code>await()</code> inside a coroutine without blocking a thread.</p>
<pre class="highlight swift"><code><span class="kd">func</span> <span class="nf">makeFutureOne</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">CoFuture</span><span class="o">&lt;</span><span class="kt">Response</span><span class="o">&gt;</span> <span class="p">{</span>
    <span class="k">let</span> <span class="nv">promise</span> <span class="o">=</span> <span class="kt">CoPromise</span><span class="o">&lt;</span><span class="kt">Response</span><span class="o">&gt;</span><span class="p">()</span>
    <span class="n">someAsyncFuncWithCallback</span> <span class="p">{</span> <span class="n">response</span> <span class="k">in</span>
        <span class="o">.</span> <span class="o">.</span> <span class="o">.</span> <span class="k">do</span> <span class="kd">some</span> <span class="n">work</span> <span class="o">.</span> <span class="o">.</span> <span class="o">.</span>
        <span class="n">promise</span><span class="o">.</span><span class="nf">success</span><span class="p">(</span><span class="n">response</span><span class="p">)</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="n">promise</span>
<span class="p">}</span>

<span class="kd">func</span> <span class="nf">makeFutureTwo</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">CoFuture</span><span class="o">&lt;</span><span class="kt">Response</span><span class="o">&gt;</span> <span class="p">{</span>
    <span class="n">queue</span><span class="o">.</span><span class="n">coroutineFuture</span> <span class="p">{</span>
        <span class="k">let</span> <span class="nv">future</span> <span class="o">=</span> <span class="nf">makeFutureOne</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
        <span class="o">.</span> <span class="o">.</span> <span class="o">.</span> <span class="k">do</span> <span class="kd">some</span> <span class="n">work</span> <span class="o">.</span> <span class="o">.</span> <span class="o">.</span>
        <span class="k">let</span> <span class="nv">response</span> <span class="o">=</span> <span class="k">try</span> <span class="n">future</span><span class="o">.</span><span class="nf">await</span><span class="p">()</span>
        <span class="o">.</span> <span class="o">.</span> <span class="o">.</span> <span class="n">create</span> <span class="n">result</span> <span class="n">using</span> <span class="n">response</span> <span class="o">.</span> <span class="o">.</span> <span class="o">.</span>
        <span class="k">return</span> <span class="n">result</span>
    <span class="p">}</span>
 <span class="p">}</span>

<span class="kd">func</span> <span class="nf">performSomeWork</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">let</span> <span class="nv">future</span> <span class="o">=</span> <span class="nf">makeFutureTwo</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
    <span class="n">mainQueue</span><span class="o">.</span><span class="n">startCoroutine</span> <span class="p">{</span>
        <span class="o">.</span> <span class="o">.</span> <span class="o">.</span> <span class="k">do</span> <span class="kd">some</span> <span class="n">work</span> <span class="o">.</span> <span class="o">.</span> <span class="o">.</span>
        <span class="k">let</span> <span class="nv">result</span> <span class="o">=</span> <span class="k">try</span> <span class="n">future</span><span class="o">.</span><span class="nf">await</span><span class="p">()</span>
        <span class="o">.</span> <span class="o">.</span> <span class="o">.</span> <span class="k">do</span> <span class="kd">some</span> <span class="n">work</span> <span class="n">using</span> <span class="n">result</span> <span class="o">.</span> <span class="o">.</span> <span class="o">.</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre>

<p>For coroutine error handling you can use standart <code>do-catch</code> statement or use <code>CoFuture</code> as an alternative.</p>
<pre class="highlight swift"><code><span class="c1">//execute coroutine and return CoFuture&lt;Void&gt; that we will use for error handling</span>
<span class="kt">DispatchQueue</span><span class="o">.</span><span class="n">main</span><span class="o">.</span><span class="n">coroutineFuture</span> <span class="p">{</span>
    <span class="k">let</span> <span class="nv">result</span> <span class="o">=</span> <span class="k">try</span> <span class="nf">makeSomeFuture</span><span class="p">()</span><span class="o">.</span><span class="nf">await</span><span class="p">()</span>
    <span class="o">.</span> <span class="o">.</span> <span class="o">.</span> <span class="n">use</span> <span class="n">result</span> <span class="o">.</span> <span class="o">.</span> <span class="o">.</span>
<span class="p">}</span><span class="o">.</span><span class="n">whenFailure</span> <span class="p">{</span> <span class="n">error</span> <span class="k">in</span>
    <span class="o">.</span> <span class="o">.</span> <span class="o">.</span> <span class="n">handle</span> <span class="n">error</span> <span class="o">.</span> <span class="o">.</span> <span class="o">.</span>
<span class="p">}</span>
</code></pre>

<p>Apple has introduced a new reactive programming framework <code>Combine</code>
that makes writing asynchronous code easier and includes a lot of convenient and common functionality.
We can use it with coroutines by making <code>CoFuture</code> a subscriber and await its result.</p>
<pre class="highlight swift"><code><span class="c1">//create Combine publisher</span>
<span class="k">let</span> <span class="nv">publisher</span> <span class="o">=</span> <span class="kt">URLSession</span><span class="o">.</span><span class="n">shared</span><span class="o">.</span><span class="nf">dataTaskPublisher</span><span class="p">(</span><span class="nv">for</span><span class="p">:</span> <span class="n">url</span><span class="p">)</span><span class="o">.</span><span class="nf">map</span><span class="p">(\</span><span class="o">.</span><span class="n">data</span><span class="p">)</span>

<span class="c1">//execute coroutine on the main thread</span>
<span class="kt">DispatchQueue</span><span class="o">.</span><span class="n">main</span><span class="o">.</span><span class="n">startCoroutine</span> <span class="p">{</span>
    <span class="c1">//subscribe CoFuture to publisher</span>
    <span class="k">let</span> <span class="nv">future</span> <span class="o">=</span> <span class="n">publisher</span><span class="o">.</span><span class="nf">subscribeCoFuture</span><span class="p">()</span>

    <span class="c1">//await data without blocking the thread</span>
    <span class="k">let</span> <span class="nv">data</span><span class="p">:</span> <span class="kt">Data</span> <span class="o">=</span> <span class="k">try</span> <span class="n">future</span><span class="o">.</span><span class="nf">await</span><span class="p">()</span>
<span class="p">}</span>
</code></pre>

                        <a href="Classes/CoFuture.html" class="slightly-smaller">See more</a>
                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">class</span> <span class="kt">CoFuture</span><span class="o">&lt;</span><span class="kt">Value</span><span class="o">&gt;</span></code></pre>
<pre class="highlight swift"><code><span class="kd">extension</span> <span class="kt">CoFuture</span><span class="p">:</span> <span class="kt">Hashable</span></code></pre>
<pre class="highlight swift"><code><span class="kd">extension</span> <span class="kt">CoFuture</span><span class="p">:</span> <span class="kt"><a href="Protocols/CoCancellable.html">CoCancellable</a></span></code></pre>

                        </div>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:14SwiftCoroutine9CoPromiseC"></a>
                    <a name="//apple_ref/swift/Class/CoPromise" class="dashAnchor"></a>
                    <a class="token" href="#/s:14SwiftCoroutine9CoPromiseC">CoPromise</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>A promise to provide a result later.</p>

<p><code>CoPromise</code> is a subclass of <code><a href="Classes/CoFuture.html">CoFuture</a></code> that allows to deliver the result.
You can set the result to <code>CoPromise</code> only once, other attempts will be ignored.</p>

                        <a href="Classes/CoPromise.html" class="slightly-smaller">See more</a>
                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">final</span> <span class="kd">class</span> <span class="kt">CoPromise</span><span class="o">&lt;</span><span class="kt">Value</span><span class="o">&gt;</span> <span class="p">:</span> <span class="kt"><a href="Classes/CoFuture.html">CoFuture</a></span><span class="o">&lt;</span><span class="kt">Value</span><span class="o">&gt;</span></code></pre>

                        </div>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:14SwiftCoroutine7CoScopeC"></a>
                    <a name="//apple_ref/swift/Class/CoScope" class="dashAnchor"></a>
                    <a class="token" href="#/s:14SwiftCoroutine7CoScopeC">CoScope</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>The holder of uncompleted <code><a href="Protocols/CoCancellable.html">CoCancellable</a></code> and coroutines.</p>

<p><code>CoScope</code> helps to manage lifecycle of coroutines and <code><a href="Protocols/CoCancellable.html">CoCancellable</a></code>, like <code><a href="Classes/CoFuture.html">CoFuture</a></code> and <code><a href="Classes/CoChannel.html">CoChannel</a></code>.
It keeps weak references on inner objects and cancels them on <code>cancel()</code> or deinit.
All completed objects are automaticaly removed from scope.</p>
<div class="aside aside-note">
    <p class="aside-title">Note</p>
    <code>CoScope</code> keeps weak references.

</div>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">scope</span> <span class="o">=</span> <span class="kt">CoScope</span><span class="p">()</span>

<span class="k">let</span> <span class="nv">future</span> <span class="o">=</span> <span class="nf">makeSomeFuture</span><span class="p">()</span><span class="o">.</span><span class="nf">added</span><span class="p">(</span><span class="nv">to</span><span class="p">:</span> <span class="n">scope</span><span class="p">)</span>

<span class="n">queue</span><span class="o">.</span><span class="nf">startCoroutine</span><span class="p">(</span><span class="nv">in</span><span class="p">:</span> <span class="n">scope</span><span class="p">)</span> <span class="p">{</span>
    <span class="o">.</span> <span class="o">.</span> <span class="o">.</span> <span class="kd">some</span> <span class="n">code</span> <span class="o">.</span> <span class="o">.</span> <span class="o">.</span>
    <span class="k">let</span> <span class="nv">result</span> <span class="o">=</span> <span class="k">try</span> <span class="n">future</span><span class="o">.</span><span class="nf">await</span><span class="p">()</span>
    <span class="o">.</span> <span class="o">.</span> <span class="o">.</span> <span class="kd">some</span> <span class="n">code</span> <span class="o">.</span> <span class="o">.</span> <span class="o">.</span>
<span class="p">}</span>

<span class="k">let</span> <span class="nv">future2</span> <span class="o">=</span> <span class="n">queue</span><span class="o">.</span><span class="n">coroutineFuture</span> <span class="p">{</span>
    <span class="k">try</span> <span class="kt">Coroutine</span><span class="o">.</span><span class="nf">delay</span><span class="p">(</span><span class="o">.</span><span class="nf">seconds</span><span class="p">(</span><span class="mi">5</span><span class="p">))</span> <span class="c1">// imitate some work</span>
    <span class="k">return</span> <span class="mi">5</span>
<span class="p">}</span><span class="o">.</span><span class="nf">added</span><span class="p">(</span><span class="nv">to</span><span class="p">:</span> <span class="n">scope</span><span class="p">)</span>

<span class="c1">//cancel all added futures and coroutines</span>
<span class="n">scope</span><span class="o">.</span><span class="nf">cancel</span><span class="p">()</span>
</code></pre>

                        <a href="Classes/CoScope.html" class="slightly-smaller">See more</a>
                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">final</span> <span class="kd">class</span> <span class="kt">CoScope</span></code></pre>
<pre class="highlight swift"><code><span class="kd">extension</span> <span class="kt">CoScope</span><span class="p">:</span> <span class="kt"><a href="Protocols/CoCancellable.html">CoCancellable</a></span></code></pre>

                        </div>
                      </div>
                    </section>
                  </div>
                </li>
              </ul>
            </div>
          </section>
        </section>
        <section id="footer">
          <p>&copy; 2020 <a class="link" href="https://github.com/belozierov/SwiftCoroutine" target="_blank" rel="external">Alex Belozierov</a>. All rights reserved. (Last updated: 2020-07-11)</p>
          <p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.13.5</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
        </section>
      </article>
    </div>
  </body>
</div>
</html>
